home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1083 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  147 lines

  1. Path: gate.compart.fi!usenet
  2. From: joonas.kervinen@pcb.compart.fi (joonas kervinen)
  3. Newsgroups: comp.lang.c
  4. Subject: Is this somekind of a stack fault or what?
  5. Date: Thu, 11 Jan 1996 20:14:40 GMT
  6. Organization: Compart BBS, Helsinki, Finland
  7. Message-ID: <4d2oi6$ghf@gate.compart.fi>
  8. NNTP-Posting-Host: spider.compart.fi
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. I have a function that causes a bizarre problem. It's used in a
  12. Windows program and a DOS version compiled with BCC 4.53 large model.
  13. Everything is just straight C. I'm pretty sure this function is the
  14. culprit since slight modifications to it (as described in the source)
  15. make everything go all right.
  16.  
  17. The function purpose is to round() a number to certain decimal
  18. accuracy. It is passed the number as a char pointer (char *sa2) and
  19. the desired decimal count (deci). While the function is not foolproof
  20. (it won't handle negative decimals or other clear errors) it is
  21. sufficient for me. I've found out that sprintf() are not reliable in
  22. rounding up situation (do a sprintf(mystr,"%f.2d",1.499) or something
  23. like that and you'll get errors)
  24.  
  25. I can't figure why the func bombs stack or whatever but it seems to
  26. overwrite memory or unbalance the stack. When I do a
  27. roundx("  9.99 ",1)
  28. I do get a pointer to "10.0". The problem is that after that strange
  29. things happen to the rest of my program. Not a big crash but some
  30. memory or pointer go away. I've carefully examined the func under
  31. Turbo debugger and it just doesn't overwrite memory. I tend to think
  32. that there is something wrong with this:
  33.  
  34. char *roundx(char *sa2, int deci)
  35. {
  36. /* */
  37. static char sa1="                   ";
  38. /* rest of program */
  39. return sa1;
  40. }
  41.  
  42. When I change it to:
  43. char *roundx(char *sa2, int deci)
  44. {
  45. /* */
  46. char sa1[20];
  47. /* rest of program with one modification*/
  48. return sa1;
  49. }
  50. ... everything works on DOS and Windows version.
  51.  
  52. Please tell me what's going on. Please answer via E-mail since I don't
  53. come to comp.lang.c often (this is also on Windows programming
  54. section)
  55.  
  56. OK. Here's the function:
  57.  
  58. char *roundx(char *sa2, int deci)
  59. {
  60. int  i, pit, up;
  61. static char sa1="                   ";
  62. //static char sa1[20];     // comment above line and uncomment this
  63.  
  64.                                    // and the func will work. 
  65.  
  66. /* find start, decimal point and dot "." */
  67. i = 0;
  68. while (1)
  69. {
  70.    if (sa2[i] != ' ')
  71.        break;
  72.    i++;
  73. }
  74. pit = 0;
  75. up = 1;
  76. //sa1[0]=' ';      //if you make corrections at the beginning 
  77.                        //uncomment this line
  78. while (1)
  79. {
  80.     if (sa2[i] == '.')
  81.          pit = up;
  82.     else
  83.     if (sa2[i] == 0)
  84.          break;
  85.     sa1[up] = sa2[i];
  86.     i++;
  87.    up++;
  88. }
  89. if (pit == 0)
  90. {                            /* desimaali ja nollat puuttuvat */
  91.    sa1[up] = '.';
  92.    memset(&sa1[up + 1], '0', deci + 1);
  93.    pit = up;
  94. }
  95. else
  96. if (up - pit - 1 < deci + 1)
  97. {                            /* Laitetaan nollia perään */
  98.    memset(&sa1[up], '0', deci + 1);
  99. }
  100. i = pit + deci;
  101. if (sa1[i + 1] - 48 > 4)
  102.    up = 1;
  103. else
  104.    up = 0;
  105. while (1)
  106. {
  107.    if (sa1[i] == '.')
  108.    {
  109.        i--;
  110.        continue;
  111.    }
  112.    else
  113.    {
  114.        if (sa1[i] - 48 + up > 9)
  115.        {
  116.           sa1[i] = '0';
  117.           i--;
  118.           continue;
  119.        }
  120.        else
  121.        {
  122.           if (sa1[i] == ' ' && up)
  123.           {
  124.               sa1[i] = '1';
  125.               break;
  126.           }
  127.           else
  128.           if (sa1[i] == '-' && up)
  129.           {
  130.               sa1[i] = '1';
  131.               sa1[i - 1] = '-';
  132.               break;
  133.           }
  134.           sa1[i] = sa1[i] + up;
  135.           break;
  136.        }
  137.    }
  138. }
  139. if (deci == 0)
  140.    sa1[pit] = 0;
  141. else
  142.    sa1[pit + 1 + deci] = 0;
  143. return sa1;
  144. }
  145.  
  146.  
  147.